package org.liveboardgames.common.utils.collections
{
    import org.liveboardgames.common.dictionary.collections.IMap;
    
    public class ModificationUtils
    {
        public static function add(map1:IMap, map2:IMap):IMap
        {
            var result:IMap = null;
            if (map1 != null || map2 != null)
            {
                // Function is symetric
                if (map1 == null)
                {
                    result = add(map2, map1);
                }
                // Nothing to really add
                else if (map2 == null)
                {
                    result = map1;
                }
                // Both have some data
                else
                {
                    // Initiate the result with data from map1
                    result = CopyUtils.cloneMap(map1);
                    for each (var key:Object in map2.getKeys())
                    {
                        var map1Value:Object = map1.getValue(key);
                        var map2Value:Object = map2.getValue(key);
                        if (map1Value != null && (map1Value is int) && (map2Value is int))
                        {
                            result.put(key, map1Value + map2Value);
                        }
                        else if (map1Value == null)
                        {
                            result.put(key, map2Value);
                        }
                        else
                        {
                            throw new Error("Not supported value: " + map1Value + " or " + map2Value);
                        }
                    }
                }
            }
            return result;
        }
    
    }
}